//
//  ViewController.m
//  22-AFNetWorking
//
//  Created by mafeng on 16/8/12.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self netWorkPOST];
}


#pragma mark -- AFNetworking POST
- (void)netWorkPOST {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURL = @"http://open3.bantangapp.com/base/app/init";
    NSString *parameters = @"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17";
    // 3. 设置请求参数的字符串格式, 自定义参数
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
        
    }];
    // 发送一个post请求
    [manager POST: postURL parameters: parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSLog(@"请⃣求⃣成⃣功⃣:%@", responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"请⃣求⃣失⃣败⃣:%@", error);
    }];
    
    
    
}



#pragma mark -- AFNetworing GET
- (void)netWorkGet {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURL = @"http://cars.app.autohome.com.cn/dealer_v6.0.0/dealer/hotbrands-pm1.json";
    
    // requestSerializer 格⃣式⃣化⃣body/参⃣数⃣
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    // 超时时间 默认 60s
    manager.requestSerializer.timeoutInterval = 90;
    // 设⃣置⃣请⃣求⃣头⃣, 设⃣置⃣requestSerializer中的mutableHTTPHeaderField一个字典
    // manager.requestSerializer setValue:<#(nullable NSString *)#> forHTTPHeaderField:<#(nonnull NSString *)#>
    
    // responseSerializer 返⃣回⃣的⃣数⃣据⃣, 进⃣行⃣格⃣式⃣化⃣
    // manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    // responseSerializer 默⃣认⃣类⃣型⃣:AFJSONResponseSerializer
    // 保⃣留⃣系⃣统⃣中⃣的⃣ContentType, 添⃣加⃣新⃣的⃣Type
    // NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
    //[set addObject:@"text/html"];
    
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]];
    
    [manager GET:requestURL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL);
        NSLog(@"请⃣求⃣成⃣功⃣:%@", responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", task.currentRequest.URL);
        NSLog(@"请⃣求⃣失⃣败⃣:%@", error);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
